﻿using AD.Pms.Api.Models;
using AD.Pms.Core;
using AD.Pms.Core.ClientApi;
using AD.Pms.Core.EnumHelper;
using AD.Pms.DataBase.Models.ZC_Config;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AD.Pms.Service.DtoModel;

namespace AD.Pms.Api.Controllers.V1
{
    [Route("api/v1/[controller]/[action]")]
    [ApiController]
    public class ConfigController : ControllerBase
    {
        private readonly ZC_configContext _zC_ConfigContext;
        private readonly Utils _utils;

        public ConfigController(ZC_configContext zC_ConfigContext, Utils utils)
        {
            _zC_ConfigContext = zC_ConfigContext;
            _utils = utils;
        }

        [HttpGet]
        public async Task<JsonResult> Categorys()
        {
            var res = new ApiResult<List<TblArticleCats>>();
            var categorys = await _zC_ConfigContext.TblArticleCats
                .Where(x => x.IsDelete == null || !x.IsDelete.Value).ToListAsync();
            res.data = categorys;
            return new JsonResult(res);
        }

        [HttpPut]
        public async Task<JsonResult> CreateCategory(TblArticleCats category)
        {
            var res = new ApiResult<TblArticleCats>();
            if (category == null)
            {
                res.statusCode = (int) ApiEnum.ParameterError;
                res.message = "类目不能为空";
                return new JsonResult(res);
            }

            if (category.ParentId.HasValue)
            {
                var entity = _zC_ConfigContext.TblArticleCats.Any(x => x.Pkid == category.ParentId.Value);
                if (!entity)
                {
                    res.statusCode = (int) ApiEnum.ParameterError;
                    res.message = "父级类目不正确";
                    return new JsonResult(res);
                }
            }

            var result = await _zC_ConfigContext.TblArticleCats.AddAsync(category);
            await _zC_ConfigContext.SaveChangesAsync();
            res.data = result.Entity;
            return new JsonResult(res);
        }

        [HttpDelete]
        public async Task<JsonResult> DeleteCategory(int id)
        {
            var res = new ApiResult<TblArticleCats>();
            var entity = _zC_ConfigContext.TblArticleCats.FirstOrDefault(x => x.Pkid == id);
            if (entity != null)
            {
                entity.IsDelete = true;
                await _zC_ConfigContext.SaveChangesAsync();
            }
            else
            {
                res.statusCode = (int) ApiEnum.ParameterError;
                res.message = "类目不存在";
            }

            return new JsonResult(res);
        }

        [HttpPost]
        public async Task<JsonResult> UpdateCategory(TblArticleCats category)
        {
            var res = new ApiResult<TblArticleCats>();
            if (category == null)
            {
                res.statusCode = (int) ApiEnum.ParameterError;
                res.message = "类目不能为空";
                return new JsonResult(res);
            }

            var entity = _zC_ConfigContext.TblArticleCats.FirstOrDefault(x => x.Pkid == category.Pkid);
            if (entity != null)
            {
                _utils.CopyPropertys(category, entity);
                await _zC_ConfigContext.SaveChangesAsync();
            }
            else
            {
                res.statusCode = (int) ApiEnum.ParameterError;
                res.message = "类目不存在";
            }

            return new JsonResult(res);
        }

        [HttpGet]
        public async Task<JsonResult> Articles(int pagesize = 10, int pageindex = 1, string keyword = "")
        {
            var res = new ApiResult<PaginatedList<TblArticle>>();
            var articles = from article in _zC_ConfigContext.TblArticle
                where article.IsDelete == null || !article.IsDelete.Value
                select article;
            if (!string.IsNullOrEmpty(keyword))
            {
                articles = articles.Where(x => x.Title.Contains(keyword) || x.Details.Contains(keyword));
            }

            articles = articles.OrderBy(x => x.Pkid);
            var data = await PaginatedList<TblArticle>.CreateAsync(articles.AsNoTracking(), pageindex, pagesize);
            res.data = data;
            return new JsonResult(res);
        }


        [HttpPut]
        public async Task<JsonResult> CreateArticle(TblArticle article)
        {
            var res = new ApiResult<TblArticle>();
            if (article == null)
            {
                res.statusCode = (int) ApiEnum.ParameterError;
                res.message = "文章不能为空";
                return new JsonResult(res);
            }

            if (article.CatId.HasValue)
            {
                var entity = _zC_ConfigContext.TblArticleCats.Any(x => x.Pkid == article.CatId.Value);
                if (!entity)
                {
                    res.statusCode = (int) ApiEnum.ParameterError;
                    res.message = "类目不正确";
                    return new JsonResult(res);
                }
            }

            var result = await _zC_ConfigContext.TblArticle.AddAsync(article);
            await _zC_ConfigContext.SaveChangesAsync();
            res.data = result.Entity;
            return new JsonResult(res);
        }

        [HttpDelete]
        public async Task<JsonResult> DeleteArticle(int id)
        {
            var res = new ApiResult<TblArticle>();
            var entity = _zC_ConfigContext.TblArticle.FirstOrDefault(x => x.Pkid == id);
            if (entity != null)
            {
                entity.IsDelete = true;
                await _zC_ConfigContext.SaveChangesAsync();
            }
            else
            {
                res.statusCode = (int) ApiEnum.ParameterError;
                res.message = "文章不存在";
            }

            return new JsonResult(res);
        }

        [HttpPost]
        public async Task<JsonResult> UpdateArticle(TblArticle article)
        {
            var res = new ApiResult<TblArticle>();
            if (article == null)
            {
                res.statusCode = (int) ApiEnum.ParameterError;
                res.message = "文章不能为空";
                return new JsonResult(res);
            }

            if (article.CatId.HasValue)
            {
                var category = _zC_ConfigContext.TblArticleCats.Any(x => x.Pkid == article.CatId.Value);
                if (!category)
                {
                    res.statusCode = (int) ApiEnum.ParameterError;
                    res.message = "类目不正确";
                    return new JsonResult(res);
                }
            }

            var entity = _zC_ConfigContext.TblArticle.FirstOrDefault(x => x.Pkid == article.Pkid);
            if (entity != null)
            {
                _utils.CopyPropertys(article, entity);
                await _zC_ConfigContext.SaveChangesAsync();
            }
            else
            {
                res.statusCode = (int) ApiEnum.ParameterError;
                res.message = "文章不存在";
            }

            return new JsonResult(res);
        }
    }
}